﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using BUS;
using System.Data.SqlClient;

namespace QLBANSACH
{
    public partial class CTHDX : Form
    {
        SqlConnection connection = DAO.sqlConnectionData.Hamketnoi();

        public CTHDX()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a = CTHDX_BUS.HienthiCTHDX().Rows.Count;
            try
            {
                CTHDX_DTO cthdx = new CTHDX_DTO(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), Convert.ToDateTime(textBox5.Text), Convert.ToInt32(textBox6.Text), Convert.ToInt32(textBox7.Text));
                CTHDX_BUS.InsertCTHDX(cthdx);
                dataGridView1.DataSource = CTHDX_BUS.HienthiCTHDX();
                int b = CTHDX_BUS.HienthiCTHDX().Rows.Count;
                if (a == b)
                {
                    MessageBox.Show("Hãy kiểm tra lại sẩn phẩm sách này trong kho!");
                }
                else
                {
                    MessageBox.Show("Thêm thành công!");
                }
            }
            catch
            {
                MessageBox.Show("Thêm thất bại!");
            }
        }

        private void CTHDX_Load(object sender, EventArgs e)
        {
            connection.Open();
            dataGridView1.DataSource = CTHDX_BUS.HienthiCTHDX();
            databinding();           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(CTHDX_BUS.Hienthi(Convert.ToInt32(textBox1.Text)).Rows[0]["SL_XS"]);
            try
            {
                CTHDX_DTO cthdx = new CTHDX_DTO(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), Convert.ToDateTime(textBox5.Text), Convert.ToInt32(textBox6.Text), Convert.ToInt32(textBox7.Text));
                CTHDX_BUS.UpdateCTHDX(cthdx);
                dataGridView1.DataSource = CTHDX_BUS.HienthiCTHDX();
                int b = Convert.ToInt32(CTHDX_BUS.Hienthi(Convert.ToInt32(textBox1.Text)).Rows[0]["SL_XS"]);
                if (a != b)
                {
                    MessageBox.Show("Sửa thành công!");
                }
                else
                {
                    MessageBox.Show("Xảy ra vấn đề ở dữ liệu, sửa không thành công!");
                }
            }
            catch
            {
                MessageBox.Show("Sửa thất bại!");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                CTHDX_DTO cthdx = new CTHDX_DTO(Convert.ToInt32(textBox1.Text));
                CTHDX_BUS.DeleteCTHDX(cthdx);
                dataGridView1.DataSource = CTHDX_BUS.HienthiCTHDX();
                MessageBox.Show("Xóa thành công!");
            }
            catch
            {
                MessageBox.Show("Xóa thất bại!");
            }
        }

        public void databinding()
        {
            textBox1.DataBindings.Clear();
            textBox1.DataBindings.Add("Text", dataGridView1.DataSource, "STT");
            textBox2.DataBindings.Clear();
            textBox2.DataBindings.Add("Text", dataGridView1.DataSource, "ID_HD");
            textBox3.DataBindings.Clear();
            textBox3.DataBindings.Add("Text", dataGridView1.DataSource, "ID_Sach");
            textBox4.DataBindings.Clear();
            textBox4.DataBindings.Add("Text", dataGridView1.DataSource, "ID_NV");
            textBox5.DataBindings.Clear();
            textBox5.DataBindings.Add("Text", dataGridView1.DataSource, "TG_XS");
            textBox6.DataBindings.Clear();
            textBox6.DataBindings.Add("Text", dataGridView1.DataSource, "SL_XS");
            textBox7.DataBindings.Clear();
            textBox7.DataBindings.Add("Text", dataGridView1.DataSource, "GiaX_Sach");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM CTHDX WHERE ID_HD = " + textBox8.Text, connection);
            DataTable tb = new DataTable();
            ad.Fill(tb);
            dataGridView1.DataSource = tb;
        }

        private void CTHDX_FormClosed(object sender, FormClosedEventArgs e)
        {
            HDX hdx = new HDX();
            hdx.Show();
        }
    }
}
